# from django.http import response
from django.http import request
from django.template import response
from rest_framework.decorators import api_view
from rest_framework.response import Response
import json
from django.core.serializers.json import DjangoJSONEncoder
from .isparkPic import draw_block_vote as draw
#从数据库里面导入表
from uccsit.models import OccupancyList, BerthBelong, BlocksCapacity, RoadShape, RoadList

# @api_view(['GET','POST'])
# def api_test(request):
#    # 获取原始数据
#     titleitems = TitleItem.objects.all()
#     data = {
#         'titleitems':[]
#     }
#     for t in titleitems:
#         data_item = {
#             'id':t.id,
#             'title':t.title,
#             'number':{
#                 'number':t.number,
#                 'tofixed':t.tofixed,
#                 'content':t.content
#             }
#         }
#         data['titleitems'].append(data_item)
    # return Response(data)

@api_view(['GET','POST'])
def get_aggregate_rates_data(request):
    data = {
        'aggregate_rate': draw.get_data_of_all_block(request.GET['datetime'], request.GET['data_scale'])
    }
    return Response(data)


@api_view(['GET','POST'])
def block_rate(request):
    block_to_be_draw = ''.join((request.GET['road_name'], request.GET['direction'], request.GET['left_or_right']))
    rate_data = draw.draw_block_rate(block_to_be_draw, request.GET['datetime'], request.GET['scale'], request.GET['version'])
    return Response(rate_data)


@api_view(['GET','POST'])
def use_rank(request):
    if request.GET['rank'] == 'use':
        query = 'SELECT A.`block_id`, ifnull(B.berth_count, 0) as used_count, A.`capacity`, ifnull(B.berth_count, 0)/A.`capacity` as rate, ifnull(B.berth_count, 0), A.`capacity`, A.`road`, A.`left_or_right`, A.`direction` ' \
                'FROM ' \
                '(SELECT `block_id`, `capacity`, `road`, `left_or_right`, `direction` FROM blocks_capacity WHERE `district`=\'南山区\') as A ' \
                'LEFT outer JOIN ' \
                '(SELECT `block_id`, COUNT(`berth`) as berth_count FROM `berth_belong` WHERE `berth`in (SELECT `berthage` FROM `occupancy_list` WHERE `in_time`< \'' + \
                request.GET['datetime'] + '\' and out_time > \'' + request.GET[
                    'datetime'] + '\') GROUP by `block_id`) as B ' \
                                  'on A.block_id= B.block_id ' \
                                  'ORDER BY `rate` desc'
    else:
        query = 'SELECT A.`block_id`, ifnull(B.berth_count, 0) as used_count, A.`capacity`, ifnull(B.berth_count, 0)/A.`capacity` as rate, ifnull(B.berth_count, 0), A.`capacity`, A.`road`, A.`left_or_right`, A.`direction` ' \
                'FROM ' \
                '(SELECT `block_id`, `capacity`, `road`, `left_or_right`, `direction` FROM blocks_capacity WHERE `district`=\'南山区\') as A ' \
                'LEFT outer JOIN ' \
                '(SELECT `block_id`, COUNT(`berth`) as berth_count FROM `berth_belong` WHERE `berth`in (SELECT `berthage` FROM `occupancy_list` WHERE `in_time`< \'' + \
                request.GET['datetime'] + '\' and out_time > \'' + request.GET[
                    'datetime'] + '\') GROUP by `block_id`) as B ' \
                                  'on A.block_id= B.block_id ' \
                                  'ORDER BY `rate` asc'
    occupancy = BlocksCapacity.objects.raw(query)
    block_used_info = list()
    for record in occupancy:
        temp_info = dict()
        temp_info['block_id'] = record.block_id
        temp_info['used_count'] = record.used_count
        temp_info['road_name'] = record.road
        temp_info['capacity'] = record.capacity
        temp_info['left_or_right'] = record.left_or_right
        temp_info['direction'] = record.direction
        temp_info['occupancy_rate'] = record.rate
        block_used_info.append(temp_info)
    return Response(block_used_info)

@api_view(['GET','POST'])
def block_vote(request):
    block_to_be_draw = ''.join((request.GET['road_name'], request.GET['direction'], request.GET['left_or_right']))
    data_buffer = draw.draw_block_vote(block_to_be_draw, version=request.GET['version'])
    return_value = json.dumps(data_buffer, cls=DjangoJSONEncoder)
    return Response(return_value)